package com.odriver.neulearn.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.odriver.neulearn.Adapter.MyExpandableListViewAdapter;
import com.odriver.neulearn.Bean.DetailCourseBean;
import com.odriver.neulearn.Bean.MenuBean;
import com.odriver.neulearn.LoginActivity;
import com.odriver.neulearn.R;
import com.odriver.neulearn.Util.SaveUtil;
import com.odriver.neulearn.Util.ScreenUtil;

import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView listview;

    private List<DetailCourseBean.PostStageResListBean> data ;
    private String COURSE_URL = "http://www.neuedu.cn/m/mobilePostCourse!findPostCourse.action";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtil.setScreenNoTitle(this);
        setContentView(R.layout.activity_main);
        initData();
    }

    /**
     * 获取数据 + 处理数据
     */
    public void initData(){
        Bundle bundle = this.getIntent().getExtras();
        final String code =  bundle.getString("id");
        RequestQueue mQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, COURSE_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        DetailCourseBean data = gson.fromJson(response, DetailCourseBean.class);
                        formalData(data.getPostStageResList());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("网络消息", error.getMessage(), error);
                    }
                }
        ) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("postid",code);
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("Cookie", SaveUtil.getCookie(MainActivity.this));
                return map;
            }
        };

        mQueue.add(stringRequest);
    }
    public void formalData(final List<DetailCourseBean.PostStageResListBean> data){
        System.out.println(data.size());
        listview  = (ExpandableListView) findViewById(R.id.idp_course_detail_expandlistview);
        listview.setAdapter(new MyExpandableListViewAdapter(this,data));
        listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Intent intent = new Intent(MainActivity.this,LearnActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("id",data.get(i).getChildStageList().get(0).getChildStageResList().get(i1).getResId());
                intent.putExtras(bundle);
                startActivity(intent);
                return true;
            }
        });
    }
}
